import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

// 解决编程式导航点击同一个按钮时会报错(vue-router 3.0.0 后面都会有这个官方bug)
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function (location) {
  return originalPush.call(this, location).catch(err => {})
};

// 路由：路由就是一种映射关系，在前端中路由的映射关系就是网页地址栏上url与视图(组件)的映射关系
const routes = [{
    path: '/',
    redirect:'/tab'
  },
  {
    path: '/tab',
    redirect:'/tab/category'
  },
  // {
  //   path: '/home',
  //   name: 'Home',
  //   component: () => import( /* webpackChunkName: "home" */ '../views/Home.vue')
  // },
  {
    path: '/about',
    name: 'About',
    component: () => import( /* webpackChunkName: "about" */ '../views/About.vue')
  },
  {
    path: '/user/:userId',
    name: 'User',
    component: () => import( /* webpackChunkName: "user" */ '../views/User.vue')
  },
  {
    path: '/tab',
    name: 'Tab',
    component: () => import( /* webpackChunkName: "tab" */ '../views/Tab.vue'),
    children:[
      {
        path:'/tab/cart',
        component: () => import(  '../components/Cart.vue')
      },
      {
        path:'/tab/category',
        component: () => import(  '../components/Category.vue')
      },
      {
        path:'/tab/user',
        component: () => import(  '../components/User.vue')
      },
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

export default router